/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ie.deloitte.cvapp.webcontrollers;

import ie.deloitte.cvapp.createcv.CreateCV;
import ie.deloitte.cvapp.domain.Employee;
import ie.deloitte.cvapp.domain.EmployeeHasSkill;
import ie.deloitte.cvapp.form.CVSearchCriteria;
import ie.deloitte.cvapp.form.EmployeeBeanForm;
import ie.deloitte.cvapp.service.CVService;
import ie.deloitte.cvapp.validators.EmployeeValidator;

import java.io.File;
import java.io.FileInputStream;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

/**
 *
 * @author damorales
 */
@Controller
@RequestMapping("/cv/*")
public class CVController {
    
    @Autowired
    private CVService cvService;
    
    private EmployeeValidator employeeValidator = new EmployeeValidator();
    

    /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     * This method is called when a request is made to display the create.jsp page
     *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
	@RequestMapping(value = "/create", method = RequestMethod.GET)
	public String setupForm(Model model, EmployeeBeanForm employee) {
		if (employee == null) {
			employee = new EmployeeBeanForm();
		}
		model.addAttribute("employee", employee);
		System.out.println("In Setup Form");
		return "cv/create";
	}
    
	/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     * This method is called when the submit button on create.jsp is pressed
     *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
	@RequestMapping(value = "/create", method = RequestMethod.POST)
	public String submitForm(
			@ModelAttribute("employee") EmployeeBeanForm employee,
			BindingResult result) {
		employeeValidator.validate(employee, result);
		if (result.hasErrors()) {
			return "/cv/create";
		} else {
			cvService.saveEmployee(employee);
			return "redirect:confirmationEmployee";
		}
	}
    
    
	/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     * This method is called when a request is made to display the confirmationEmployee.jsp page
     *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
	@RequestMapping(value = "/confirmationEmployee", method = RequestMethod.GET)
	public String confirmation() {
		return "cv/confirmationEmployee";
	}
	
	/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     * This method is called when a request is made to display the rejectionEmployee.jsp page
     *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
	@RequestMapping(value = "/rejectionEmployee", method = RequestMethod.GET)
	public String rejection() {
		return "cv/rejectionEmployee";
	}
    
	/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     * This method is called when a request is made to display the validationFailed.jsp page
     *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
	@RequestMapping(value = "/validationFailed", method = RequestMethod.GET)
	public String validationFailed() {
		return "cv/validationFailed";
	}
    
    //show?employeeid=�394�
     @RequestMapping(value = "/show", method = RequestMethod.GET)
    public String showDetails(@RequestParam ("employeeid") String employeeid, Model model) {
    	// http://localhost:8080/cvproject/cv/show?employeeid=%E2%80%98394%E2%80%99
         //TODO get the model and add the employeeId
    	 //Employee = Employee(employeeid);
    	 //model.containsAttribute(employeeid); 
    	 Employee employee = cvService.findEmployeeById(employeeid);
    	 model.addAttribute("employee", employee);
         System.out.print("showDetails"+employeeid);
         return "cv/show";
    }
     
      @RequestMapping(value = "/search", method = RequestMethod.GET)
    public String search() {
        
        return "cv/search";
    }
      
         @RequestMapping(value = "/update", method = RequestMethod.GET)
    public String update(@RequestParam("employeeid") String employeeid, Model model) {
        	 model.addAttribute("employee", this.cvService.findEmployeeById(employeeid));
        return "cv/update";
    }
         
         /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
          * This method is called when the update button on update.jsp is pressed
          *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
            @RequestMapping(value = "/update", method = RequestMethod.POST)
            public String updateForm(@ModelAttribute("employee") Employee employee, BindingResult result) {
                   //System.out.println("Employee updated");
                   //System.out.println("Name " + employee.getName());
                   EmployeeBeanForm employeeBeanForm = new EmployeeBeanForm();
                   
             employeeBeanForm.setName(employee.getName());
             employeeBeanForm.setSurname(employee.getSurname());
             employeeBeanForm.setProfile(employee.getProfile());
             employeeBeanForm.setRoleproposed(employee.getRoleproposed());
             employeeBeanForm.setCreationdate(employee.getCreationdate());
             employeeBeanForm.setEmail(employee.getEmail());
             employeeBeanForm.setPrimarytelephone(employee.getPrimarytelephone());
             employeeBeanForm.setSecondarytelepohne(employee.getSecondarytelepohne());
                   
                   employeeValidator.validate(employeeBeanForm, result);
                   if (result.hasErrors()) {
                          return "cv/update";
                   } else {
                          cvService.updateEmployee(employee);
                          return "redirect:list";
                   }
                   
            }

     
      @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list(Model model) {
        
          model.addAttribute("employees",this.cvService.getAllEmployees());
          
        return "cv/list";
    }
      

      @RequestMapping(value = "/newt", method = RequestMethod.GET)
      public void test(@RequestParam("employeeid") String employeeid,
                    HttpServletResponse response) {

             Employee employee = cvService.findEmployeeById(employeeid);
             System.out.println(employeeid);
             response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
             File file = null;
             try {

                    file = File.createTempFile("wordDoc", "tmp");
                    CreateCV creator = new CreateCV();
                    creator.create(file, employee);

                    FileInputStream in = new FileInputStream(file);
                    IOUtils.copy(in, response.getOutputStream());

             } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
             } finally {
                    if (file == null) {
                          file.delete();
                    }
             }
      }

      @RequestMapping(value = "/generate", method = RequestMethod.GET)
      public String generate() {

             return "cv/generate";
      }

	  	/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     * This method is called when a request is made to display the employeeSkillUpdate.jsp page
     *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
	@RequestMapping(value = "/employeeSkillUpdate", method = RequestMethod.GET)
	public String employeeSkillUpdate(@RequestParam String employeeId, Model model) {

		model.addAttribute("employee", cvService.findEmployeeById(employeeId));
		model.addAttribute("skills", cvService.getAllSkills());
		model.addAttribute("employeeHasSkill", new EmployeeHasSkill());
		return "cv/employeeSkillUpdate";
	}
	
	/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     * This method is called when the update button on employeeSkillUpdate.jsp is pressed
     *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
	@RequestMapping(value = "/employeeSkillUpdate", method = RequestMethod.POST)
	public String  employeeSkillUpdateForm(@ModelAttribute("employeeHasSkill") EmployeeHasSkill employee) {
		cvService.saveEmployeeHasSkill(employee);
		return "redirect:list";
	}

	/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     * This method is called when a request is made to display the employeeSkillUpdate.jsp page
     *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
	@RequestMapping(value = "/showSkills", method = RequestMethod.GET)
	public String showSkills(@RequestParam String employeeId, Model model) {

		model.addAttribute("employee", cvService.findEmployeeById(employeeId));
//		model.addAttribute("skills", cvService.getAllSkills());
	//	model.addAttribute("employeeskills", cvService.getAllSkillsByEmployeeId(employeeId));
	//	List<Skills> skills = this.cvService.getAllEmployees();		
	//	model.addAttribute("employeeHasSkill", new EmployeeHasSkill());
		return "cv/showSkills";
	}


}



